package org.hot100_1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class leetcode14 {
    public int[][] merge(int[][] intervals) {
        //根据左边界进行排序

        Arrays.sort(intervals, (Arrays1, Arrays2) -> Arrays1[0]-Arrays2[0]);

        int left =intervals[0][0];
        int right = intervals[0][1];
        List<int []> resList = new ArrayList<>();

        for (int i = 1; i < intervals.length; i++) {
            //新数组的左边界大于原来的右边界
            if (intervals[i][0]>right){
                int [] temp = {left,right};
                resList.add(temp);
                left=intervals[i][0];
                right=intervals[i][1];
            }else {
                //新数组的左边界小于原来的右边界
                right=Math.max(right,intervals[i][1]);
            }
        }
        int [] temp = {left,right};
        resList.add(temp);

        int [][] res=new int[resList.size()][2];

        for (int i = 0; i < res.length; i++) {
            res[i]=resList.get(i);
        }

        return res;
    }
}
